#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "I2C_Hard.h"
#include "OLED_I2C_HARD.h"
#include "stdio.h"
#include "Mpu6050_I2C_HARD.h"
#include "Uart.h"
#include "DMA_UART1.h"
#include "Timer.h"


static mpu6050_data * mpu6050_raw_data;

int main(void)
{
    // 配置 NVIC 中段优先级
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    init_i2c();
	init_oled();
    init_mpu_6050();
    
    init_dma_for_uart1();
    
    init_uart_with_dma();
 
 
    timer_init();
    
    
    char str[50];
    
    Integrated_Euler_Angles  integrated_euler_angles;
    
	while (1)
	{
        /*
        calculate_euler_angles(&integrated_euler_angles);
        sprintf(str,"%5f,%5f,%5f,",integrated_euler_angles.roll,integrated_euler_angles.pitch,integrated_euler_angles.yaw);
        send((uint8_t *)str,sizeof(str));
        */
        
        
        
   
        
        sprintf(str,"%7d %7d",mpu6050_raw_data->x_acc,mpu6050_raw_data->x_gyr);
        send((uint8_t *)str,sizeof(str));
		show_string(1,1,(uint8_t *)str);
        
        sprintf(str,"%7d %7d",mpu6050_raw_data->y_acc,mpu6050_raw_data->y_gyr);
        send((uint8_t *)str,sizeof(str));
		show_string(2,1,(uint8_t *)str);
        
        sprintf(str,"%7d %7d",mpu6050_raw_data->z_acc,mpu6050_raw_data->z_gyr);
        send((uint8_t *)str,sizeof(str));
		show_string(3,1,(uint8_t *)str);
	
    }
}

void TIM2_IRQHandler(void){
    static char str[50];
    if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET){
        
        mpu6050_raw_data =  read_6_axises();
       
        sprintf(str,"%7d,%7d,%7d,%7d,%7d,%7d,",
            mpu6050_raw_data->x_acc,
            mpu6050_raw_data->y_acc,
            mpu6050_raw_data->z_acc,
            mpu6050_raw_data->x_gyr,
            mpu6050_raw_data->y_gyr,
            mpu6050_raw_data->z_gyr
        );
        send((uint8_t *)str,sizeof(str));
        TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
    }
}
